package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.AssessScoreBean;
import com.hxkcloud.patient.viewholders.AssessScoresViewHolder;

import java.util.List;

/**
 * Created by william_sun on 2016-10-12.
 * Description:专科评估（调查问卷）得分列表
 */
public class AssessScoresAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<AssessScoreBean> datas;
    private LayoutInflater mInflater;
    private onAssessScoreSelectListener listener;

    public AssessScoresAdapter(Context mContext, List<AssessScoreBean> datas, onAssessScoreSelectListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        mInflater = LayoutInflater.from(mContext);
        this.listener = listener;
    }

    public void setData(List<AssessScoreBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_assessscores, null);
        AssessScoresViewHolder holder = new AssessScoresViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        AssessScoresViewHolder mHolder = (AssessScoresViewHolder) holder;
        AssessScoreBean data = datas.get(position);
        mHolder.rl_whole.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelect(position);
            }
        });
        if (TextUtils.isEmpty(data.getAnswerType())) {
            mHolder.iv_type.setImageResource(R.drawable.list_sick_logo);
            mHolder.tv_name.setText(R.string.patientassess);
        } else if (data.getAnswerType().equals("3")) {
            mHolder.iv_type.setImageResource(R.drawable.list_sick_logo);
            mHolder.tv_name.setText(R.string.patientassess);
        } else {
            mHolder.iv_type.setImageResource(R.drawable.list_nurse_logo);
            mHolder.tv_name.setText(data.getNurseName().substring(0, 1) + "护士");
        }
        if (TextUtils.isEmpty(data.getDate())) {
            mHolder.tv_date.setText("");
        } else {
            mHolder.tv_date.setText(data.getDate().split(" ")[0]);
        }
        if (!TextUtils.isEmpty(data.getScore())) {
            mHolder.tv_scorekind1.setVisibility(View.VISIBLE);
            mHolder.tv_score.setVisibility(View.VISIBLE);
            mHolder.tv_scorekind2.setVisibility(View.VISIBLE);
            mHolder.tv_scorekind1.setText(R.string.scorekind1start);
            mHolder.tv_score.setText(data.getScore());
            mHolder.tv_scorekind2.setText(R.string.scorekind1end);

            mHolder.tv_noanswer.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(data.getGrade())) {
            mHolder.tv_scorekind1.setVisibility(View.VISIBLE);
            mHolder.tv_score.setVisibility(View.VISIBLE);
            mHolder.tv_scorekind2.setVisibility(View.VISIBLE);
            mHolder.tv_scorekind1.setText(R.string.scorekind2start);
            mHolder.tv_score.setText(data.getGrade());
            mHolder.tv_scorekind2.setText(R.string.scorekind2end);

            mHolder.tv_noanswer.setVisibility(View.GONE);
        } else if (data.getIsAnswer().equals("0")) {
            mHolder.tv_scorekind1.setVisibility(View.GONE);
            mHolder.tv_score.setVisibility(View.GONE);
            mHolder.tv_scorekind2.setVisibility(View.GONE);

            mHolder.tv_noanswer.setVisibility(View.VISIBLE);
        } else {
            mHolder.tv_scorekind1.setVisibility(View.GONE);
            mHolder.tv_score.setVisibility(View.GONE);
            mHolder.tv_scorekind2.setVisibility(View.GONE);

            mHolder.tv_noanswer.setVisibility(View.GONE);
        }

        if (position == datas.size() - 1) {
            mHolder.view_divider.setVisibility(View.GONE);
        } else {
            mHolder.view_divider.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface onAssessScoreSelectListener {
        void onSelect(int position);
    }
}
